package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.StrUtil;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.DeleteOssFileForm;
import com.example.emos.api.oss.ConstantPropertiesUtil;
import com.example.emos.api.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author jl
 * @since 2021/11/6 16:04
 */
@RestController
@RequestMapping("/oss")
@Slf4j
@Tag(name = "OssController", description = "对象存储web接口")
public class OssController {
    @Autowired
    private FileService fileService;

    @PostMapping("/uploadFile")
    @SaCheckLogin
    @Operation(summary = "上传文件")
    public R uploadFile(@RequestParam("file")MultipartFile file, @RequestParam(value = "folder", required = false)String folder) {
        // 如果传了folder,就存储到指定文件夹目录下
        if (StrUtil.isNotBlank(folder)) {
            ConstantPropertiesUtil.FOLDER = folder;
        }
        String uploadUrl = fileService.upload(file);
        return R.ok("文件上传成功").put("url", uploadUrl);
    }

    @PostMapping("/deleteFile")
    @SaCheckLogin
    @Operation(summary = "删除文件")
    public R deleteFile(@RequestBody DeleteOssFileForm fileForm) {
        fileService.delete(fileForm.getPaths());
        return R.ok();
    }


}
